﻿@using SIGBugs.Shared.Models
@using SIGBugs.Shared.ViewModels
@using Microsoft.AspNetCore.Blazor.Browser.Interop;
@page "/fetchtask/{projectId}"
@inject HttpClient Http
@inject Microsoft.AspNetCore.Blazor.Services.IUriHelper UriHelper


@if (taskList == null)
{
    <p><em>Loading...</em></p>
}
else
{
    <h1>当前项目：@taskList.Project.Name</h1>
    <p>@taskList.Project.Description</p>
    <p class="text-right">
        <a href="/addtask" class="btn btn-primary btn-sm"><span class="oi oi-plus"></span> 发布任务</a>
    </p>
    <table class='table'>
        <thead>
            <tr>
                <th>ID</th>
                <th>任务</th>
                <th>状态</th>
                <th>创建时间</th>
                <th>创建者</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var emp in taskList.Tasks)
            {
            <tr>
                <td>@emp.Id</td>
                <td>@emp.Title <span>@emp.TypeText</span> </td>
                <td>@emp.StatusText</td>
                <td>@string.Format("{0:d}", emp.CreatedDate)</td>
                <td>@emp.CreatedBy</td>
                <td>
                    <a href='/editproject/@emp.Id' class='btn btn-primary btn-sm'>编辑</a>
                    <a href='/delete/@emp.Id' class='btn btn-danger btn-sm'>删除</a>
                </td>
            </tr>
            }
        </tbody>
    </table>
}

@functions {
[Parameter]
string projectId { get; set; }

PageTaskVM taskList;
protected override async System.Threading.Tasks.Task OnInitAsync()
{
    var token = SIGBugs.Client.Libs.JsInterop.GetToken();
    // RegisteredFunction.Invoke<bool>("log", token);
    if (string.IsNullOrEmpty(token))
    {
        UriHelper.NavigateTo("/login");
    }
    else
    {

        try
        {
            Http.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
            taskList = await Http.GetJsonAsync<PageTaskVM>("/api/Tasks/GetTasks/" + Convert.ToInt32(projectId));
        }
        catch (Exception er)
        {
            RegisteredFunction.Invoke<bool>("log", er.Message);
            UriHelper.NavigateTo("/login");
        }
    }

}


}